//
//  FighterPrototypeParser.cpp
//  airfight
//
//  Created by aaron more on 12-8-28.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include <iostream>
#include <map>
#include "FighterPrototypeParser.h"



FighterPrototypeParser::FighterPrototypeParser()
:prototypes(NULL),
isStart(false),
currentFighter(NULL)
{
    
}

FighterPrototypeParser::~FighterPrototypeParser()
{
    CC_SAFE_RELEASE(currentFighter);
    CC_SAFE_RELEASE(prototypes);
}

FighterPrototypeParser * FighterPrototypeParser::formatWithXMLFile(const char *xmlFile)
{
    FighterPrototypeParser *pRet = new FighterPrototypeParser();
    if(pRet->initWithXMLFile(xmlFile))
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}

void FighterPrototypeParser::internalInit(const char* tmxFileName)
{
    isStart = false; 
    prototypes = FighterPrototypes::getInstance();
    prototypes->retain();
    if (tmxFileName != NULL)
    {
        m_sXMLFileName = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(tmxFileName);
    }
}

void FighterPrototypeParser::startElement(void *ctx, const char *name, const char **atts)
{
    
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>();
    if(atts && atts[0])
    {
        for(int i = 0; atts[i]; i += 2) 
        {
            std::string key = (char*)atts[i];
            std::string value = (char*)atts[i+1];
            attributeDict->insert(std::pair<std::string, std::string>(key, value));
        }
    }
    if("fighter" == elementName )
    {
        Fighter* fighter = new Fighter();
        //        int i = 0;
        //        sscanf(valueForKey("id", attributeDict), "%d", &i);
        fighter->setFighterId((int)atoi(valueForKey("id", attributeDict)));
        fighter->setBlood((int)atoi(valueForKey("blood", attributeDict)));
        fighter->setArmor((int)atoi(valueForKey("armor", attributeDict)));
        fighter->setSpeed((int)atoi(valueForKey("speed", attributeDict)));
        fighter->setRes(valueForKey("res", attributeDict));
        if (!currentFighter) 
        {
            currentFighter = fighter;
        }
    }
    else if("bullet" == elementName) 
    {
        if(currentFighter)
        {
            
            Bullet* bullet = new Bullet();
            bullet->setID((int)atoi(valueForKey("id", attributeDict)));
            bullet->setRes(valueForKey("res", attributeDict));
            bullet->setFrequency((int)atoi(valueForKey("frequency", attributeDict)));
            bullet->setSpeed((int)atoi(valueForKey("speed", attributeDict)));
            bullet->setDuration((float)atof(valueForKey("duration", attributeDict)));
            bullet->setDamage((int)atoi(valueForKey("damage", attributeDict)));
            bullet->setType((int)atoi(valueForKey("type", attributeDict)));
            bullet->setAngle((int)atoi(valueForKey("angle", attributeDict)));
            bullet->setGroup_num((int)atoi(valueForKey("group_num", attributeDict)));
            bullet->setGroup_interval((int)atoi(valueForKey("group_interval", attributeDict)));
            bullet->setBegin((int)atoi(valueForKey("begin", attributeDict)));
            bullet->setFirePoint((int)atoi(valueForKey("posx", attributeDict)), (int)atoi(valueForKey("posy", attributeDict)));
            currentFighter->addBullet(bullet);
            bullet->release();
            
        }
    }
}

void FighterPrototypeParser::endElement(void *ctx, const char *name)
{
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    if("fighter" == elementName)
    {
        prototypes->addFighter(currentFighter);
        currentFighter->release();
        currentFighter = NULL;
    }
}

void FighterPrototypeParser::textHandler(void *ctx, const char *s, int len)
{
    
}

